<?php
namespace Home\Controller;
use Common\Controller\HomeBaseController;
class UserController extends HomeBaseController {
    // 定义数据表
    private $db;
    private $viewDb;

    // 构造函数 实例化UserModel表
    public function __construct(){
        parent::__construct();
        $this->db=D('User');
    }


    // 第三方平台登录
    public function oauth_login(){
        $type=I('get.type');
        import("Org.ThinkSDK.ThinkOauth");
        $sdk=\ThinkOauth::getInstance($type);
        redirect($sdk->getRequestCodeURL());
    }

    // 用户注册
    public function register(){

        if ( IS_POST ) {

            $email     = I('post.email','','trim');
            $password  = I('post.password', '', '');
            $repassword  = I('post.repassword', '', '');
            $username  = I('post.username', '', 'htmlspecialchars');

            empty($email) && $this->error('邮箱不能为空');
            empty($password) && $this->error('密码不能为空');
            empty($repassword) && $this->error('请再输入一遍密码');
            empty($username) && $this->error('用户名不能为空');

            if ($password!=$repassword) {
                $this->error('两次输入的密码不一致');
            }

            if (!isValidEmail($email)) {
                $this->error('邮箱格式不正确');
            }
            if (!isValidName($username)) {
                $this->error('用户名只能包含中文、英文、数字、下划线');
            }

            $uid =$this->db->insert($email, $password, $username);

            if ( $uid ) {

                $user= $this->db->where(array('uid'=>$uid))->find();
                $this->db->autoLogin($user);
                $this->success('注册成功');

            } else {
                $this->error('对不起，邮箱或昵称已经被占用');
            }

        } else {
            $this->assign( 'title', '注册' );
            $this->display();
        }
    }


    // 用户登录
    public  function login() {

        if ( IS_POST ) {
            $email     = I('post.email','','trim');
            $password  = I('post.password', '', '');
            empty($email) && $this->error('邮箱不能为空');
            empty($password) && $this->error('密码不能为空');

            $User =  D( 'User' );
            $user =  $User->login( $email, $password );
            if ( $user ) {

                $this->success('登录成功');
            } else {
                $this->error( $User->getError() );
            }
        } else {
            if ($mid==1) {
                $this->redirect('index/index');
            }
            $this->assign( 'title', '登录' );
            $this->display();
        }

    }


    // 退出登录
    public function logout() {
        if(isset($_SESSION['user']['uid'])){

            session( 'user', null );
            session( 'user_sign', null );
            session('[destroy]');
            //cookie('talkpiece_uid', null);
            //setcookie('syncuyan', 'logout', time() + 3600, '/', 'blog.jiathis.com');
            $this->success("操作完成！");
        } else {
            $this->error('已经退出登录！');
        }
    }



    //重置密码
    public function repassword() {
        if ( IS_POST ) {
            $email  = I('post.email', '', '');
            $user   = D( 'User')->where( array( 'email'=>$email ) )->find();
            if (!empty($user) ) {
                $result =D( 'UserToken' )->sendUserToken( $user['email'], $user['uid'], $user['username'], 2 );
                if ( $result ) {
                    $this->error( '重置密码邮件发送成功，请接收' );
                } else {
                    $this->error( '邮件发送失败' );
                }
            } else {
                $this->error( '用户不存在' );
            }
        } else {
            $this->display();
        }

    }

    // 判断是否登录
    public function check_login(){
        if(isset($_SESSION['user'])){
            echo 1;
        }else{
            echo 0;
        }
    }
}
